package com.visikard.ui.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.View;

import com.visikard.application.MyApplication;
import com.visikard.application.UIMessage;
import com.visikard.asynctask.BaseTask;
import com.visikard.asynctask.LogoutTask;
import com.visikard.ui.R;
import com.visikard.ui.fragment.MainHomeFragment;
import com.visikard.ui.fragment.RedeemptionFragment;
import com.visikard.ui.fragment.ViewClickable;

public class MainHomeActivity extends BaseLoginCompleteActivity implements ViewClickable {

	public static final String TAG_CONTENT_FRAGMENT = "MainMenuBaseActivity_tag_left_content_fragmetn";

	public static final int DLG_CONFIRM_EXIT = 0x1;
	public static final int DLG_ENABLE_GPS = 0x2;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		
		setContentView(R.layout.main_content);
		
		Fragment contentFrag = new MainHomeFragment();
		contentFrag.setArguments(getIntent().getExtras());			
		setHomeContent(contentFrag);
	}

	public void setHomeContent( Fragment contentFragment) {
		
		getSupportFragmentManager().beginTransaction().replace(R.id.main_content, contentFragment, TAG_CONTENT_FRAGMENT).commit();
	}
	
	public Fragment getHomeFragment() {
		return getSupportFragmentManager().findFragmentByTag(TAG_CONTENT_FRAGMENT);
	}
	
	@Override
	public void onViewClick( View v ) {		
		Object contentFragment = getHomeFragment();
		if( contentFragment != null && (contentFragment instanceof ViewClickable) ) {
			((ViewClickable)contentFragment).onViewClick(v);
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DLG_CONFIRM_EXIT:
			return new AlertDialog.Builder(this)
					.setTitle(R.string.main_screen_dlg_exit_title)
					.setMessage(R.string.main_screen_dlg_exit_msg)
					.setPositiveButton(android.R.string.yes,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									finish();
								}
							})
					.setNegativeButton(getString(android.R.string.no), null)
					.create();

		case DLG_ENABLE_GPS:
			return new AlertDialog.Builder(this)
					.setTitle(R.string.dlg_no_gps_title)
					.setMessage(R.string.dlg_no_gps_msg)
					.setPositiveButton(R.string.dlg_no_gps_enable,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									startActivity(new Intent(
											android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
								}
							})
					.setNegativeButton(R.string.dlg_no_gps_not_now, null)
					.create();
		}
		return null;
	}		
	
	public void displayConfirmLogout() {

		AlertDialog alerDialod = new AlertDialog.Builder(MainHomeActivity.this)
				.setTitle(R.string.logout_btn_logout)
				.setMessage(R.string.logout_title_logout)
				.setPositiveButton(R.string.logout_btn_logout,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								BaseTask mAsyncTask = new LogoutTask(mHandler);
								mAsyncTask.execute();
							}
						})
				.setNegativeButton(getString(R.string.literal_cancel), null)
				.create();
		alerDialod.show();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			showExitDialog();
			return true;
		}
		return false;
	}
	
	private void showExitDialog(){
		AlertDialog dialog = new AlertDialog.Builder(this).setTitle(R.string.main_screen_dlg_exit_title)
				.setMessage(R.string.main_screen_dlg_exit_msg)
				.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				}).setNegativeButton(getString(android.R.string.no), null).create();
		dialog.show();
	}
	
	public void mainPutFragment(Fragment fm, String tab){
		((RedeemptionFragment)getHomeFragment()).removeFragment();
		((RedeemptionFragment)getHomeFragment()).putFragment(fm, tab);
	}
	
	@Override
	public boolean handleMessage(Message msg) {		
		switch (msg.what) {
		case UIMessage.MSG_LOGOUT:
			((MyApplication)getApplication()).logout();
			dismissProgressDialog();
			Intent intent;
			intent = new Intent(MainHomeActivity.this, VKLoginActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			finish();
			overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);
			return true;
		}
		return super.handleMessage(msg);
	}
}
